package com.kaikai.edusta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kaikai.commonutils.R;
import com.kaikai.edusta.client.UcenterClient;
import com.kaikai.edusta.entity.StatisticsDaily;
import com.kaikai.edusta.mapper.StatisticsDailyMapper;
import com.kaikai.edusta.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author yujiankai
 * @since 2021-06-20
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    //分析表格数据查询
    @Override
    public void createStatisticsByDate(String day) {
        //删除已存在的统计对象
        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.eq("date_calculated", day);
        baseMapper.delete(dayQueryWrapper);
        //获取统计信息
        Integer registerNum = (Integer) ucenterClient.getRegCountByDay(day).getData().get("countRegister");
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO
        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);
        baseMapper.insert(daily);
    }

    //根据条件生成统计表
    //获取xy轴坐标信息:时间,数量
    @Override
    public Map createStaByCondition(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        //设置查询列:xy轴数据
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        /**
         * 由于前端需要数组形式数据,而list返回后为数组形式.
         * 实例对象和map集合返回数据均为json对象形式
         * 这里创建两个list,用于返回所需数据
         */
        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for (StatisticsDaily sta :
                staList) {
            dateList.add(sta.getDateCalculated());
            switch (type) {
                case "login_num":
                    numList.add(sta.getLoginNum());
                    break;
                case "register_num":
                    numList.add(sta.getRegisterNum());
                    break;
                case "video_view_num":
                    numList.add(sta.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(sta.getCourseNum());
                    break;
            }
        }
        Map map=new HashMap();
        map.put("dateList",dateList);
        map.put("numList",numList);
        return map;
    }
}
